package com.javasm.gg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.process.ProcessService;
import com.javasm.process.entity.ProcessTask;
import com.javasm.process.entity.TaskStatus;
import com.javasm.sys.CurrentLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("prices")
public class PriceAdjustController extends BaseController {

    @Resource
   private  PriceAdjustService adjustPriceService;

    //存草稿
    @PostMapping
    public AxiosResult addPriceAdjust(@RequestBody PriceAdjust priceAdjust) {
        priceAdjust.setProcessStatus(TaskStatus.ORDER_CG.getStatus());
        priceAdjust.setApplyUser(CurrentLogin.getLoginUserName());
        boolean flag = adjustPriceService.save(priceAdjust);
        return isok(flag);
    }

    //启动流程
    @PostMapping("start")
    public AxiosResult addAndStartProcess(@RequestBody PriceAdjust priceAdjust) {
        priceAdjust.setProcessStatus(TaskStatus.ORDER_APPROVE_ING.getStatus());
        priceAdjust.setApplyUser(CurrentLogin.getLoginUserName());
        boolean flag = adjustPriceService.saveAndStart(priceAdjust);
        return isok(flag);
    }

    //流程跟踪
    @GetMapping("/audit/{orderId}")
    public AxiosResult getTasks(@PathVariable String orderId) {
        List<ProcessTask> tasks = adjustPriceService.getTaskByApplyOrderId(orderId);
        return suc(tasks);
    }

    //查询代办
    @GetMapping("todos")
    public TableDatas getToDoTasks(){
        startPage();
        List<ProcessTask> tasks = adjustPriceService.queryToDoTasks();
//
//        List<String> applyIds = tasks.stream().map(ProcessTask::getApplyId).collect(Collectors.toList());
//
//        List<PriceAdjust> list = this.adjustPriceService.listPriceAdjuctAndGoodsByIds(applyIds);
        return toTableDatas(tasks);
    }

    //查询已办
    @GetMapping("dos")
    public TableDatas getdosWtgTasks(){
        startPage();
        List<ProcessTask> tasks = adjustPriceService.queryDosTasks(null);
//        List<String> applyIds = tasks.stream().map(ProcessTask::getApplyId).collect(Collectors.toList());
//        List<PriceAdjust> list = this.adjustPriceService.listPriceAdjuctAndGoodsByIds(applyIds);
        return toTableDatas(tasks);
    }

    @GetMapping("claim/{orderId}")
    public AxiosResult doClaim(@PathVariable String orderId){
        boolean f = adjustPriceService.claim(orderId);
        return isok(f);
    }

    //审批
    @PostMapping("doapprove")//做审批
    public AxiosResult doApprove(@RequestBody ProcessTask task){
        adjustPriceService.approve(task);
        return suc();
    }

    @GetMapping
    public TableDatas getTableData(PriceAdjust priceAdjust) {
        startPage();
        List<PriceAdjust> list = adjustPriceService.listPriceAdjuctAndGoods(priceAdjust);
        return new TableDatas(list);
    }

    @GetMapping("{id}")
    public AxiosResult getPriceAdjustById(@PathVariable String id) {
        PriceAdjust p = adjustPriceService.getPriceAdjuctAndGoodsById(id);
        return suc(p);
    }

    @PutMapping
    public AxiosResult updatePriceAdjust(@RequestBody PriceAdjust priceAdjust) {
       boolean flag = adjustPriceService.updateById(priceAdjust);
        return isok(flag);
    }

    @DeleteMapping("{id}")
    public AxiosResult deletePriceAdjust(@PathVariable String id) {
        boolean flag = adjustPriceService.removeById(id);
        return isok(flag);
    }
}

